﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;
using OpenSSL.Crypto;
using System.IO;
using OpenSSL.Core;

namespace Sicurezza.CA.Manager
{
    public class KeyManager
    {
        // TODO: richiamare la chiave dal manager apposito
        private static CryptoKey privateKey = null;
        public static CryptoKey PrivateKey
        {
            get
            {
                if (privateKey == null)
                {
                    string keyPath = HttpContext.Current.Request.PhysicalApplicationPath;
                    keyPath = Path.Combine(keyPath,
                        ConfigurationManager.AppSettings["private"]);
                    privateKey = CryptoKey.FromPrivateKey(BIO.File(keyPath, "r"), null);
                }
                return privateKey;


            }
            set { privateKey = value; }
        }

        public static string GetRSAModulus()
        {
            return PrivateKey.GetRSA().PublicModulus.ToDecimalString();
        }

        public static string GetRSAPrivateExponent()
        {
            return PrivateKey.GetRSA().PrivateExponent.ToDecimalString();
        }
    }
}
